<?php
class App_User_Service_DbStructure extends Main_Service_DbStructureAbstract
{
	/**
	 * @var App_User_Service_DbStructure
	 */
	private static $_instance;

	/**
	 * @return App_User_Service_DbStructure
	 */
	public static function getInstance() 
	{
		if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
	}
	
	/**
	 * @param string $driver
	 * @param string $driverType
	 * @return string
	 */
	public function getInstallSql($driver, $driverType = null)
	{
		// --- mysql --- {{
		if ($driver == 'mysql') 
		{
			// --- mysql myisam --- {{
			if ($driverType = 'myisam') 
			{
				$sql = <<<SQL
drop table if exists user;
create table user
(
   id                   int unsigned not null auto_increment,
   login                varchar(100) not null,
   password             varchar(32) not null,
   email                varchar(255) default NULL,
   role_id              tinyint unsigned not null,
   f_active             tinyint(0) unsigned not null,
   primary key (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

drop table if exists user_i18n;
create table user_i18n
(
   id                   int unsigned not null auto_increment,
   user_id              int unsigned not null,
   lang_id              tinyint unsigned not null,
   first_name           varchar(100) default NULL,
   second_name          varchar(100) default NULL,
   middle_name          varchar(100) default NULL,
   primary key (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

drop table if exists role;
create table role
(
   id                   tinyint unsigned not null auto_increment,
   code                 varchar(100) not null,
   primary key (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

drop table if exists role_i18n;
create table role_i18n
(
   id                   tinyint unsigned not null auto_increment,
   role_id              tinyint unsigned not null,
   lang_id              tinyint unsigned not null,
   name                 varchar(255) not null,
   primary key (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
SQL;
			}
			// --- mysql myisam --- }}
		}
		// --- mysql --- }}
		
		return $sql;		
	}
	
	/**
	 * @return void
	 */
	public function setDefaultData()
	{
		$langId = Zend_Registry::getInstance()->langId;
		
		// --- admin role and admin user --- {{
		$this->db->insert('role', array("code"=>"'" . Main_Const::ADMIN_CODE . "'"));
		$roleId = $this->db->lastInsertId('role');
		$this->db->insert('role_i18n', array(
			"role_id" => $roleId,
		 	"lang_id" => $langId,
		 	"name"	  => "'Администратор'"
		));

		$this->db->insert('user', array(
			"login"		=> "'" . Main_Const::ADMIN_LOGIN . "'",
			"password"	=> "'" . Main_Function::encodePassword('123') . "'",
			"role_id"	=> $roleId,
			"f_active"	=> 1,	
		));
		$userId = $this->db->lastInsertId('user');	
		$this->db->insert('user_i18n', array(
			"user_id"	=> $userId,
			"lang_id"	=> $langId,
		));
		// --- admin role and admin user --- }}
		
	}
	
	
	
	
	
}
?>